package com.ztesoft.web.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.baseconfig.db.dao.DmPlanDao;
import com.ztesoft.web.common.db.dao.DmHostConnInfoDao;
import com.ztesoft.web.common.db.po.DmHostConnInfoPO;
import com.ztesoft.web.common.exchange.ExchangeFunc;
import com.ztesoft.web.filecommon.db.dao.DmSrcFileDao;
import com.ztesoft.web.filecommon.db.dao.FileDirectoryDefineDao;
import com.ztesoft.web.filecommon.db.dao.FileScanRuleInstanceDao;
import com.ztesoft.web.filecommon.db.dao.XpathVariantRelDao;
import com.ztesoft.web.filecommon.db.po.DmSrcFilePO;
import com.ztesoft.web.filecommon.db.po.FileDirectoryDefinePO;
import com.ztesoft.web.filecommon.db.po.FileScanRuleInstancePO;
import com.ztesoft.web.filecommon.db.po.XpathVariantRelPO;
import com.ztesoft.web.syncbaseinfo.service.BaseInfoService;


/**
 * shell操作命令获取
 *
 */
@Service
public class ShellCommandDefine {
	
	@Autowired 
	private DmHostConnInfoDao dmHostConnInfoDao;
	
	@Autowired 
	private DmSrcFileDao dmSrcFileDao;
	
	@Autowired 
	private FileDirectoryDefineDao fileDirectoryDefineDao;
	
	@Autowired 
	private XpathVariantRelDao xpathVariantRelDao;
	
	@Autowired
	private FileScanRuleInstanceDao fileScanRuleInstanceDao;
	
	@Autowired
	private ExchangeFunc exchangeFunc;
	
	@Autowired
	private BaseInfoService baseInfoService;
	
	@Autowired
	private DmPlanDao dmPlanDao;
	/**
	 * 日志
	 */
    private final static ZTEsoftLogManager LOGGER = ZTEsoftLogManager
            .getLogger(ShellCommandDefine.class);
    
    /**
     * 初始化
     * @param nodeId	{DM_PLAN.NODE_ID}所属系统模块标志
     * @return	分表集合
     */
	public List<String> initFileName(String pathExp, int dirOrder, int sourceDataId, int nodeId, int planId, int dataType) {
		List<String> srcXpathList = new ArrayList<String>();
		Map<Integer, List<String>> defineMap = new HashMap<Integer, List<String>>();
		// 系统标志获取
		nodeId = baseInfoService.getRootLevelSysModule(nodeId);
		//4、获取变量值
		defineMap = tokenStr(sourceDataId, nodeId, planId, dataType, dirOrder);
		//5、变量值实际值获取
		srcXpathList = replaceVaria(defineMap, pathExp, dirOrder);
		//6、无效目录清理 TODO
		return srcXpathList;
	}
	
	private Map<Integer, List<String>> tokenStr(int sourceDataId, int nodeId, int planId, int dataType, int dirOrder){ 
		Map<Integer, List<String>> tokens = new HashMap<Integer,List<String>>();
		List<String> list = new ArrayList<String>();
		int dloop = 0;
		StringBuilder sb = new StringBuilder();
		List<String> lis = new ArrayList<String>();
		List<XpathVariantRelPO> relList = xpathVariantRelDao.selectPOByDirOrder(sourceDataId, dataType, dirOrder);
		Date date = dmPlanDao.selectByPrimaryKey(planId).getCreateDate();
		for (XpathVariantRelPO variantRel : relList) {
			Integer defineType = variantRel.getVariantDefineType();
			List<String> variantDefineType = new ArrayList<String>();
			Integer lowerInt = 0;
			Integer upperInt = 0;
			if(1 == defineType){
				if(Utils.notEmpty(variantRel.getVariantDefineValue())){
					List<Integer> billList = Utils.convertIdInteger2ListByComma(variantRel.getVariantDefineValue());
					upperInt = billList.get(0);//数据类型
					lowerInt = billList.get(1);
				}
				//所属系统
				switch (variantRel.getFuncId()) {
				case 1://按本地网
					list = exchangeFunc.latnIdExchange(nodeId);
					break;
				case 2:// 按年月->往前偏移
					list = exchangeFunc.yearMonthExchange(date, upperInt, lowerInt);
					break;
				case 3://按年月日->往前偏移
					list = exchangeFunc.dateExchange(date, upperInt, lowerInt);
					break;
				case 4://按账期->往前偏移
					list = exchangeFunc.billingCycleExchange(date, upperInt, lowerInt);
					break;
				case 5://按客户分组
					list = exchangeFunc.custCategoryExchange(nodeId);
					break;
				case 6://按清单序列
					list = exchangeFunc.detailSeqExchange(nodeId);
					break;
				case 7://按进程号分组
					list = exchangeFunc.pidExchange(nodeId);
					break;
				default:
					break;
				}
				if (Utils.notEmpty(list)) {			
					sb.append("{").append(dloop++).append("}").append("_");
					variantDefineType.add(Utils.convertIdList2StringByComma(list));
				}
			}
			if(2 == defineType){
				variantDefineType = Utils.convertIdString2ListByComma(variantRel.getVariantDefineValue());
			}
			tokens.put(variantRel.getDirOrder(), variantDefineType);
		}
		return tokens;
	}
	/**
	 * 变量值实际值获取
	 * @param tokensMap
	 * @param proStr
	 * @return
	 */
	private List<String> replaceVaria(Map<Integer, List<String>> tokensMap, String proStr, Integer dirOrder){
		String variant_ = "";
		List<String> varList = new ArrayList<String>();
		List<String> result = new ArrayList<String>();
		if(tokensMap.size()<1){
			result.add(proStr);
		}
		for(int i=0; i<tokensMap.size();i++){
			variant_ = setFixed(i+1);
			varList = tokensMap.get(dirOrder);
			for (String var : varList) {
				result.addAll(turnfileDef(proStr, variant_, var));
			}
		}
		return result;
		
	}

	private String setFixed(Integer fixed) {
		return "${"+fixed+"}";
	}
	
	private List<String> turnfileDef(String proStr, String variantStr,String tokenStr) {
		String valus ="";
		List<String> proStrList = new ArrayList<String>();
		List<String> token = Utils.convertIdString2ListByComma(tokenStr);
		if(Utils.notEmpty(token)){
			for (String string : token) {
				valus = proStr.replace(variantStr, string);
				proStrList.add(valus);
			}
		}else{
			proStrList.add(proStr);
		}
		return proStrList;
	}
	
}
